{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 目标\n",
    "\n",
    "1. 预测：把原图切成cut_size1大小的图片，需要补全，归一化后切成cut_size2大小的图片，需要补全\n",
    "2. 后期处理：把预测出来的cut_size2大小的图片合成cut_size1大小，然后再合成原图大小"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 思路\n",
    "\n",
    "1. 把原图切成cut_size1大小的图片，右边、下面和右下角可能有边角需要补全，归一化后切成cut_size2大小的图片，右边、下面和右下角可能有边角需要补全\n",
    "2. 把预测出来的cut_size2大小的图片合成cut_size1大小，有多余的部分需要去掉，然后再合成原图大小"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 导包和读取原图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4000, 15106, 4)\n"
     ]
    }
   ],
   "source": [
    "import cv2\n",
    "import tifffile as tiff\n",
    "import os\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "%matplotlib inline\n",
    "\n",
    "# FILE_2015 = '../preliminary/quickbird2015_preliminary_2.tif'\n",
    "# FILE_2017 = '../preliminary/quickbird2017_preliminary_2.tif'\n",
    "# final阶段图片\n",
    "FILE_2015 = '../preliminary/quarterfinals_2015.tif'\n",
    "FILE_2017 = '../preliminary/quarterfinals_2017.tif'\n",
    "img_2015 = tiff.imread(FILE_2015).transpose([1, 2, 0])\n",
    "img_2017 = tiff.imread(FILE_2017).transpose([1, 2, 0])\n",
    "print img_2015.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 调用的归一化函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def scale_percentile_n(matrix):\n",
    "    w, h, d = matrix.shape\n",
    "    matrix = np.reshape(matrix, [w * h, d]).astype(np.float64)\n",
    "    # Get 2nd and 98th percentile\n",
    "    mins = np.percentile(matrix, 1, axis=0)\n",
    "#     print mins\n",
    "    maxs = np.percentile(matrix, 99, axis=0) - mins\n",
    "#     print maxs\n",
    "    matrix = (matrix - mins[None, :]) / maxs[None, :]\n",
    "    matrix = np.reshape(matrix, [w, h, d])\n",
    "    matrix = matrix.clip(0, 1)\n",
    "    return matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 把原图切成960大小补全后归一化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4000, 15106, 3)\n"
     ]
    }
   ],
   "source": [
    "# 设置通道数\n",
    "channel = [0, 1, 2]\n",
    "# channel = [0, 1, 2]\n",
    "img_2015 = img_2015[:,:,channel]\n",
    "img_2017 = img_2017[:,:,channel]\n",
    "print img_2015.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Generation completed\n"
     ]
    }
   ],
   "source": [
    "IM_ROWS = img_2015.shape[0]\n",
    "IM_COLS = img_2015.shape[1]\n",
    "cut_size1 = 960\n",
    "\n",
    "DATA_DIR = 'output'\n",
    "\n",
    "if not os.path.exists(DATA_DIR + '/train960_normalize'):\n",
    "    os.makedirs(DATA_DIR + '/train960_normalize')\n",
    "\n",
    "years = [2015, 2017]\n",
    "\n",
    "for year in years:\n",
    "    img = eval('img_{}'.format(year))\n",
    "    for i in xrange(int(IM_ROWS // cut_size1)):\n",
    "        for j in xrange(int(IM_COLS // cut_size1)):\n",
    "            ss = '{}/train960_normalize/{}_{}_{}_{}.jpg'.format(DATA_DIR, year, i, j, cut_size1)\n",
    "            if os.path.exists(ss):\n",
    "                continue\n",
    "            tiff.imsave(ss, np.array(scale_percentile_n(img[i*cut_size1:(i+1)*cut_size1,j*cut_size1:(j+1)*cut_size1])*255, dtype=np.uint8))\n",
    "\n",
    "    if IM_ROWS%cut_size1 != 0:\n",
    "        i += 1\n",
    "        for k in xrange(int(IM_COLS // cut_size1)):\n",
    "            ss = '{}/train960_normalize/{}_{}_{}_{}.jpg'.format(DATA_DIR, year, i, k, cut_size1)\n",
    "            if os.path.exists(ss):\n",
    "                continue\n",
    "            tiff.imsave(ss, np.array(scale_percentile_n(img[(IM_ROWS-cut_size1):IM_ROWS, k*cut_size1:(k+1)*cut_size1])*255, dtype=np.uint8))\n",
    "\n",
    "    if IM_COLS%cut_size1 != 0:\n",
    "        j += 1\n",
    "        for k in xrange(int(IM_ROWS // cut_size1)):\n",
    "            ss = '{}/train960_normalize/{}_{}_{}_{}.jpg'.format(DATA_DIR, year, k, j, cut_size1)\n",
    "            if os.path.exists(ss):\n",
    "                continue\n",
    "            tiff.imsave(ss, np.array(scale_percentile_n(img[k*cut_size1:(k+1)*cut_size1, (IM_COLS-cut_size1):IM_COLS])*255, dtype=np.uint8))\n",
    "\n",
    "    if IM_ROWS%cut_size1 != 0 and IM_COLS%cut_size1 != 0:\n",
    "        ss = '{}/train960_normalize/{}_{}_{}_{}.jpg'.format(DATA_DIR, year, i, j, cut_size1)\n",
    "        if os.path.exists(ss):\n",
    "                continue\n",
    "        tiff.imsave(ss, np.array(scale_percentile_n(img[(IM_ROWS-cut_size1):IM_ROWS, (IM_COLS-cut_size1):IM_COLS])*255, dtype=np.uint8))\n",
    "        \n",
    "print('Generation completed')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 把cut_size1大小的图片切成cut_size2大小的图片，需要补全"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'NoneType' object has no attribute '__getitem__'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-49-c3ecf7755cf3>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     12\u001b[0m                 \u001b[0;32mfor\u001b[0m \u001b[0my\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mxrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcut_size1\u001b[0m \u001b[0;34m//\u001b[0m \u001b[0mcut_size2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     13\u001b[0m                     \u001b[0mss1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'{}/result_image/{}_{}_{}_{}_{}_{}.tif'\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mDATA_DIR\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0myear\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcut_size2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 14\u001b[0;31m                     \u001b[0mtiff\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mimsave\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mss1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimg\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mcut_size2\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mcut_size2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mcut_size2\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0my\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mcut_size2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0muint8\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     15\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     16\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0mcut_size1\u001b[0m\u001b[0;34m%\u001b[0m\u001b[0mcut_size2\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mTypeError\u001b[0m: 'NoneType' object has no attribute '__getitem__'"
     ]
    }
   ],
   "source": [
    "cut_size2 = 160\n",
    "\n",
    "if not os.path.exists(DATA_DIR + '/result_image'):\n",
    "    os.makedirs(DATA_DIR + '/result_image')\n",
    "\n",
    "for year in years:\n",
    "    for i in xrange(int(IM_ROWS // cut_size1)+1):\n",
    "        for j in xrange(int(IM_COLS // cut_size1)+1):\n",
    "            ss = '{}/train960_normalize/{}_{}_{}_{}.tif'.format(DATA_DIR, year, i, j, cut_size1)\n",
    "            img = cv2.imread(ss, -1)\n",
    "            for x in xrange(int(cut_size1 // cut_size2)):\n",
    "                for y in xrange(int(cut_size1 // cut_size2)):\n",
    "                    ss1 = '{}/result_image/{}_{}_{}_{}_{}_{}.tif'.format(DATA_DIR, year, i, j, x, y, cut_size2)\n",
    "                    tiff.imsave(ss1, np.array(img[x*cut_size2:(x+1)*cut_size2, y*cut_size2:(y+1)*cut_size2], dtype=np.uint8))\n",
    "\n",
    "            if cut_size1%cut_size2 != 0:\n",
    "                x += 1\n",
    "                y += 1\n",
    "                for k in xrange(int(cut_size1 // cut_size2)):\n",
    "                    ss1 = '{}/result_image/{}_{}_{}_{}_{}_{}.tif'.format(DATA_DIR, year, i, j, x, k, cut_size2)\n",
    "                    tiff.imsave(ss1, np.array(img[(cut_size1-cut_size2):cut_size1, k*cut_size2:(k+1)*cut_size2], dtype=np.uint8))\n",
    "                    ss2 = '{}/result_image/{}_{}_{}_{}_{}_{}.tif'.format(DATA_DIR, year, i, j, k, y, cut_size2)\n",
    "                    tiff.imsave(ss2, np.array(img[k*cut_size2:(k+1)*cut_size2, (cut_size1-cut_size2):cut_size1], dtype=np.uint8))\n",
    "\n",
    "                ss3 = '{}/result_image/{}_{}_{}_{}_{}_{}.tif'.format(DATA_DIR, year, i, j, x, y, cut_size2)\n",
    "                tiff.imsave(ss3, np.array(img[(cut_size1-cut_size2):cut_size1, (cut_size1-cut_size2):cut_size1], dtype=np.uint8))\n",
    "\n",
    "print('Generation completed')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 生成对应的掩膜图片假数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Generation completed\n"
     ]
    }
   ],
   "source": [
    "if not os.path.exists(DATA_DIR + '/result_pred'):\n",
    "    os.makedirs(DATA_DIR + '/result_pred')\n",
    "    \n",
    "for year in years:\n",
    "    for i in xrange(int(IM_ROWS // cut_size1)+1):\n",
    "        for j in xrange(int(IM_COLS // cut_size1)+1):\n",
    "            for x in xrange(int(cut_size1 // cut_size2)):\n",
    "                for y in xrange(int(cut_size1 // cut_size2)):\n",
    "                    ss1 = '{}/result_image/{}_{}_{}_{}_{}_{}.tif'.format(DATA_DIR, year, i, j, x, y, cut_size2)\n",
    "                    ss = '{}/result_pred/{}_{}_{}_{}_{}_{}.tif'.format(DATA_DIR, year, i, j, x, y, cut_size2)\n",
    "                    img = tiff.imread(ss1)\n",
    "                    tiff.imsave(ss, np.array(img[:,:,0]>128, dtype=np.uint8))\n",
    "\n",
    "            if cut_size1%cut_size2 != 0:\n",
    "                x += 1\n",
    "                y += 1\n",
    "                for k in xrange(int(cut_size1 // cut_size2)):\n",
    "                    ss1 = '{}/result_image/{}_{}_{}_{}_{}_{}.tif'.format(DATA_DIR, year, i, j, x, k, cut_size2)\n",
    "                    ss = '{}/result_pred/{}_{}_{}_{}_{}_{}.tif'.format(DATA_DIR, year, i, j, x, y, cut_size2)\n",
    "                    img = tiff.imread(ss1)\n",
    "                    tiff.imsave(ss, np.array(img[:,:,0]>128, dtype=np.uint8))\n",
    "                    ss2 = '{}/result_image/{}_{}_{}_{}_{}_{}.tif'.format(DATA_DIR, year, i, j, k, y, cut_size2)\n",
    "                    ss = '{}/result_pred/{}_{}_{}_{}_{}_{}.tif'.format(DATA_DIR, year, i, j, x, y, cut_size2)\n",
    "                    img = tiff.imread(ss2)\n",
    "                    tiff.imsave(ss, np.array(img[:,:,0]>128, dtype=np.uint8))\n",
    "\n",
    "                ss3 = '{}/result_image/{}_{}_{}_{}_{}_{}.tif'.format(DATA_DIR, year, i, j, x, y, cut_size2)\n",
    "                ss = '{}/result_pred/{}_{}_{}_{}_{}_{}.tif'.format(DATA_DIR, year, i, j, x, y, cut_size2)\n",
    "                img = tiff.imread(ss3)\n",
    "                tiff.imsave(ss, np.array(img[:,:,0]>128, dtype=np.uint8))\n",
    "\n",
    "print('Generation completed')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 合并成cut_size1大小的图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Generation completed\n"
     ]
    }
   ],
   "source": [
    "import cv2\n",
    "import tifffile as tiff\n",
    "import os\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "cut_size2 = 160\n",
    "\n",
    "%matplotlib inline\n",
    "\n",
    "DATA_DIR = 'output'\n",
    "if not os.path.exists(DATA_DIR+'/result_pred960'):\n",
    "    os.makedirs(DATA_DIR+'/result_pred960')\n",
    "\n",
    "for year in years:\n",
    "    img = eval('img_{}'.format(year))\n",
    "    for i in xrange(int(IM_ROWS // cut_size1) +1 ):\n",
    "        for j in xrange(int(IM_COLS // cut_size1) + 1):\n",
    "            img_pred = np.ones((cut_size1, cut_size1))\n",
    "            ss = '{}/result_pred960/{}_{}_{}_{}.tif'.format(DATA_DIR, year, i, j, cut_size1)\n",
    "            if os.path.exists(ss):\n",
    "                continue\n",
    "            for x in xrange(int(cut_size1 // cut_size2)):\n",
    "                for y in xrange(int(cut_size1 // cut_size2)):\n",
    "                    ss1 = '{}/result_pred/{}_{}_{}_{}_{}_{}.tif'.format(DATA_DIR, year, i, j, x, y, cut_size2)\n",
    "                    img_pred[x*cut_size2:(x+1)*cut_size2, y*cut_size2:(y+1)*cut_size2] = tiff.imread(ss1)\n",
    "\n",
    "            if cut_size1%cut_size2 != 0:\n",
    "                x += 1\n",
    "                y += 1\n",
    "                for k in xrange(int(cut_size1 // cut_size2)):\n",
    "                    ss1 = '{}/result_pred/{}_{}_{}_{}_{}_{}.tif'.format(DATA_DIR, year, i, j, x, k, cut_size2)\n",
    "                    img_pred[(cut_size1-cut_size2):cut_size1, k*cut_size2:(k+1)*cut_size2] = tiff.imread(ss1)\n",
    "                    ss2 = '{}/result_pred/{}_{}_{}_{}_{}_{}.tif'.format(DATA_DIR, year, i, j, k, y, cut_size2)\n",
    "                    img_pred[k*cut_size2:(k+1)*cut_size2, (cut_size1-cut_size2):cut_size1] = tiff.imread(ss1)\n",
    "\n",
    "                ss3 = '{}/result_pred/{}_{}_{}_{}_{}_{}.tif'.format(DATA_DIR, year, i, j, x, y, cut_size2)\n",
    "                img_pred[(cut_size1-cut_size2):cut_size1, (cut_size1-cut_size2):cut_size1] = tiff.imread(ss1)\n",
    "            tiff.imsave(ss, img_pred)\n",
    "        \n",
    "print('Generation completed')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 把cut_size1大小的图片合成一张整图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Generation completed\n"
     ]
    }
   ],
   "source": [
    "# if not os.path.exists(DATA_DIR + '/train960_normalize'):\n",
    "#     os.makedirs(DATA_DIR + '/train960_normalize')\n",
    "\n",
    "for year in years:\n",
    "    img = np.ones((IM_ROWS, IM_COLS))\n",
    "    ss = '{}/tiny_{}.tif'.format(DATA_DIR, str(year)[-2:])\n",
    "    for i in xrange(int(IM_ROWS // cut_size1)):\n",
    "        for j in xrange(int(IM_COLS // cut_size1)):\n",
    "            ss1 = '{}/result_pred960/{}_{}_{}_{}.tif'.format(DATA_DIR, year, i, j, cut_size1)\n",
    "            img[i*cut_size1:(i+1)*cut_size1,j*cut_size1:(j+1)*cut_size1] = tiff.imread(ss1)\n",
    "\n",
    "    if IM_ROWS%cut_size1 != 0:\n",
    "        i += 1\n",
    "        for k in xrange(int(IM_COLS // cut_size1)):\n",
    "            ss1 = '{}/result_pred960/{}_{}_{}_{}.tif'.format(DATA_DIR, year, i, k, cut_size1)\n",
    "            img[i*cut_size1:IM_ROWS, k*cut_size1:(k+1)*cut_size1] = tiff.imread(ss1)[cut_size1-(IM_ROWS-i*cut_size1):, :]\n",
    "\n",
    "    if IM_COLS%cut_size1 != 0:\n",
    "        j += 1\n",
    "        for k in xrange(int(IM_ROWS // cut_size1)):\n",
    "            ss1 = '{}/result_pred960/{}_{}_{}_{}.tif'.format(DATA_DIR, year, k, j, cut_size1)\n",
    "            img[k*cut_size1:(k+1)*cut_size1, j*cut_size1:IM_COLS] = tiff.imread(ss1)[:, cut_size1-(IM_COLS-j*cut_size1):]\n",
    "\n",
    "    if IM_ROWS%cut_size1 != 0 and IM_COLS%cut_size1 != 0:\n",
    "        ss1 = '{}/result_pred960/{}_{}_{}_{}.tif'.format(DATA_DIR, year, i, j, cut_size1)\n",
    "        img[i*cut_size1:IM_ROWS, j*cut_size1:IM_COLS] = tiff.imread(ss1)[cut_size1-(IM_ROWS-i*cut_size1):, cut_size1-(IM_COLS-j*cut_size1):]\n",
    "    tiff.imsave(ss, np.array(img*255, dtype=np.uint8))\n",
    "        \n",
    "print('Generation completed')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
